﻿// Copyright (c) Inossem. All rights reserved.
// Licensed under the LGPL-3.0 license. See LICENSE file in the project root for full license information.
using DWF.Studio.Custom;
using DWF.Studio.Host;
using DWF.Studio.Project;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using WpfMessageBoxLibrary;

namespace DWF.Studio.Views
{
    /// <summary>
    /// UCtrlSnippetExplorer.xaml 的交互逻辑
    /// </summary>
    public partial class UCtrlSnippetExplorer : UserControl
    {
        /// <summary>
        /// 远程片段宿主
        /// </summary>
        private RemoteSnippetExplorerHost _snippetExplorerHost = null;
        public UCtrlSnippetExplorer()
        {
            InitializeComponent();

            if (!DesignerProperties.GetIsInDesignMode(this))
            {
                init();
            }
        }
        /// <summary>
        /// 初始化
        /// </summary>
        private void init()
        {
            GlobalActionHandler.ProjectOpened += loadSnippetExplorer;
            GlobalActionHandler.ProjectClosing += closeSnippetExplorer;

            GlobalActionHandler.PackageChanging += closeSnippetExplorer;
            GlobalActionHandler.PackageChanged += loadSnippetExplorer;
            GlobalActionHandler.SnippetReload += snippetReload;
            GlobalActionHandler.IsCoverSnippet += isCoverSnippet;
        }

        /// <summary>
        /// 加载工具栏
        /// </summary>
        /// <param name="e"></param>
        private void loadSnippetExplorer(EventArgs e)
        {
            var loaderHost = RemoteLoaderHost.GetCurrentInstance();//获取当前远程控件加载器

            WindowLoading.SetContent(LocalizationProvider.GetLocalizedString("WindowLoading_Msg_LoadActivity"));
            _snippetExplorerHost = new RemoteSnippetExplorerHost(loaderHost.LoadSnippetProxy());
            borderSnippetBorder.Child = _snippetExplorerHost?.GetSnippetExplorer();
        }

        /// <summary>
        /// 关闭项目
        /// </summary>
        /// <param name="e"></param>
        private void closeSnippetExplorer(EventArgs e)
        {
            borderSnippetBorder.Child = null;
            _snippetExplorerHost?.Dispose();
            _snippetExplorerHost = null;
        }

        public void Refresh()
        {
            this.borderSnippetBorder.Child?.Refresh();
        }
        private void snippetReload(EventArgs e)
        {
            _snippetExplorerHost.Reload();
        }
        private void isCoverSnippet(TagEventArgs<string,string,string> e)
        {
            var msgProperties = new WpfMessageBoxProperties()
            {
                Button = MessageBoxButton.YesNo,
                ButtonYesText = LocalizationProvider.GetLocalizedString("MessageBox_Button_Yes"),
                ButtonNoText = LocalizationProvider.GetLocalizedString("MessageBox_Button_No"),
                Image = MessageBoxImage.Question,
                Text = string.Format(LocalizationProvider.GetLocalizedString("UCtrlProjectExplorer_Snippets_MessageBox_CoverFile"), e.Tag1),
                Title = LocalizationProvider.GetLocalizedString("UCtrlDesignRegion_MsgBox_Caption_CloseApp_ExecutingNotice"),
            };

            Window owner = Window.GetWindow(this);
            MessageBoxResult dlgResult = WpfMessageBox.Show(owner, ref msgProperties);

            if (dlgResult == MessageBoxResult.Yes)
                _snippetExplorerHost.coverSnippet(e.Tag2,e.Tag3);
        }
    }
}
